import { CircleShape } from "../../lib/kgame/d2/shape/CircleShape";
import { ECSCmp } from "../../lib/kgame/ecs/ECSCmp";
import { ColliderCmp } from "./ColliderCmp";
import { SnakeHeadCmp } from "./SnakeHeadCmp";

/**
 * 蛇身组件
 */
export class SnakeBodyCmp extends ECSCmp {

    /** 蛇头组件 */
    head: SnakeHeadCmp;
    /** 碰撞组件 */
    collider: ColliderCmp<CircleShape>;
    /** 在蛇的第几节 */
    idx: number;

    /**
     * 初始化蛇身组件
     * @param head 蛇头组件
     * @param collider 碰撞器组件
     */
    onCreate(head: SnakeHeadCmp, collider: ColliderCmp<CircleShape>) {
        this.head = head;
        this.collider = collider;
        this.idx = head.bodys.length;
        collider.enable = head.ghostTime <= 0;
        return this;
    }

    protected onClear(): void {
        this.head = null;
        this.collider = null;
    }

}